package com.design.patterns.list.iterator.aggregate;

import com.design.patterns.list.iterator.concreteiterator.ComputerCollegeIterator;

import java.util.Iterator;

/**
 * 具体的聚合类
 * @author Wangzhiwen
 */
public class ComputerCollege implements College {

    // 聚合具体的元素，在这里规定其数据存储结构
    private Department[] departments;
    private int numberOfDepartment;

    public ComputerCollege(int size) {
        departments = new Department[size];
    }

    @Override
    public String name() {
        return "计算机学院";
    }

    // 添加元素 ， 暴露了具体的存储结构
/*    @Override
    public void add(Department department) {
        // 保存元素
        departments[numberOfDepartment] = department;
        // 更新下一个元素index
        numberOfDepartment++;
    }*/

    // 添加元素，客戶端不依賴Department
    @Override
    public void add(String name, String desc) {
        Department department = new Department(name, desc);
        // 保存元素
        departments[numberOfDepartment] = department;
        // 更新下一个元素index
        numberOfDepartment++;
    }

    // 返回元素迭代器
    @Override
    public Iterator<Department> createIterator() {
        return new ComputerCollegeIterator(departments);
    }
}
